package org.jsola.hr.web;

import org.jsola.admin.entity.DictItemDO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.ZtcConfigAddDTO;
import org.jsola.hr.dto.ZtcConfigUpdateDTO;
import org.jsola.hr.dto.syncSocDTO.QdAttritionCheckDTO;
import org.jsola.hr.dto.syncSocDTO.QdEmploymentRegistrationUnit;
import org.jsola.hr.dto.syncSocDTO.QdIncrementCheckDTO;
import org.jsola.hr.dto.syncSocDTO.ZtcAccessTokenDTO;
import org.jsola.hr.provider.IAdminProviderService;
import org.jsola.hr.query.ZtcConfigQuery;
import org.jsola.hr.service.IZtcConfigService;
import org.jsola.hr.vo.ZtcConfigListVO;
import org.jsola.hr.vo.ZtcConfigVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import javax.validation.Valid;


/**
 * 政企直通车配置
 *
 * @author zhr
 */
@RestController("hrZtcConfigController")
@RequestMapping(HrConstants.API_PATH + "/v1/ztcConfigs")
public class ZtcConfigController implements IZtcConfigControllerAPI {

    @Autowired
    private IZtcConfigService ztcConfigService;

    @Autowired
    private IAdminProviderService adminProviderService;

    @Override
    @PostMapping
    public Result<ZtcConfigVO> save(@Valid @RequestBody ZtcConfigAddDTO ztcConfigAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增政企直通车配置权限校验

        return Result.success(
                ztcConfigService.save(ztcConfigAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ZtcConfigUpdateDTO ztcConfigUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改政企直通车配置权限校验

        int count = ztcConfigService.update(ztcConfigUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{ztcConfigId}")
    public Result<Integer> delete(@PathVariable String ztcConfigId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除政企直通车配置权限校验

        int count = ztcConfigService.deleteById(ztcConfigId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] ztcConfigIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除政企直通车配置权限校验

        int count = ztcConfigService.deleteByIds(tokenUser, ztcConfigIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{ztcConfigId}")
    public Result<ZtcConfigVO> findById(@PathVariable String ztcConfigId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看政企直通车配置权限校验

        return Result.success(
                ztcConfigService.selectById(ztcConfigId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ZtcConfigListVO>> listAll(@Valid ZtcConfigQuery ztcConfigQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看政企直通车配置权限校验

        return Result.success(
                ztcConfigService.select(ztcConfigQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<ZtcConfigListVO>> page(@Valid ZtcConfigQuery ztcConfigQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                ztcConfigService.selectPage(ztcConfigQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/selectPageConfig")
    public Result<Page<ZtcConfigListVO>> selectPageConfig(@Valid ZtcConfigQuery ztcConfigQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                ztcConfigService.selectPageConfig(ztcConfigQuery, tokenUser)
        );
    }

    @Override
    @GetMapping("/ztcAccessTokenExist/{companyId}")
    public Result<Boolean> ztcAccessTokenExist(@PathVariable String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                ztcConfigService.ztcAccessTokenExist(companyId)
        );
    }

    @Override
    @PostMapping("/saveCacheAccessToken")
    public Result<String> saveCacheAccessToken(@RequestBody ZtcAccessTokenDTO ztcAccessTokenDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        ztcConfigService.saveCacheAccessToken(ztcAccessTokenDTO);
        return Result.success();
    }

    @Override
    @PostMapping("/checkEmpQdWebSite")
    public Result<List<QdIncrementCheckDTO>> checkEmpQdWebSite(@RequestBody ZtcAccessTokenDTO ztcAccessTokenDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验


        return Result.success(
                ztcConfigService.checkEmpQdWebSite(ztcAccessTokenDTO, tokenUser)
        );
    }

    @Override
    @PostMapping("/checkDecreaseEmpQdWebSite")
    public Result<List<QdAttritionCheckDTO>> checkDecreaseEmpQdWebSite(@RequestBody ZtcAccessTokenDTO ztcAccessTokenDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验


        return Result.success(
                ztcConfigService.checkDecreaseEmpQdWebSite(ztcAccessTokenDTO, tokenUser)
        );
    }

    @Override
    @GetMapping("/registrationUnit/{areaId}")
    public Result<List<QdEmploymentRegistrationUnit>> getEmploymentRegistrationUnit(@PathVariable String areaId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验


        return Result.success(
                ztcConfigService.getEmploymentRegistrationUnit(tokenUser, areaId)
        );
    }

    @Override
    @GetMapping("/selectByDictCode")
    public Result<List<DictItemDO>> selectByDictCode(@RequestParam("dictCode") String dictCode) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验


        return Result.success(
                adminProviderService.selectByDictCode(dictCode)
        );
    }

    @Override
    @GetMapping("/selectDictByParentId")
    public Result<List<DictItemDO>> selectDictByParentId(@RequestParam("dictCode") String dictCode,
                                                         @RequestParam("parentId") String parentId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验


        return Result.success(
                adminProviderService.selectDictByParentId(dictCode, parentId)
        );
    }

    @Override
    @GetMapping("/selectLikeByCodeAndName")
    public Result<List<DictItemDO>> selectLikeByCodeAndName(@RequestParam("value") String value,
                                                            @RequestParam("dictCode") String dictCode) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                adminProviderService.selectLikeByCodeAndName(value, dictCode)
        );
    }

    @Override
    @GetMapping("/getWebSiteCookie/{areaId}")
    public Result<String> getWebSiteCookie(@PathVariable String areaId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                ztcConfigService.getWebSiteCookie(areaId, tokenUser)
        );
    }
}

